﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SerialPortSendTest
{
    public partial class Form1 : Form
    {
        public Form1(string serialPortName)
        {
            InitializeComponent();
            this.serialPortName = serialPortName;
        }

        private string serialPortName;
        Random random = new Random();

        private byte GetRandomNum(int max)
        {
            Thread.Sleep(5);
            return Encoding.ASCII.GetBytes(random.Next(0, max).ToString())[0];
        }

       

        private byte[] GetBuffer()
        {
            byte[] buffer = new byte[12];

            buffer[0] = 02;
            buffer[1] = random.Next(0, 2) == 1 ? Encoding.ASCII.GetBytes("+")[0] : Encoding.ASCII.GetBytes("-")[0];

            //构造重量数据
            buffer[2] = Encoding.ASCII.GetBytes("0")[0];
            buffer[3] = Encoding.ASCII.GetBytes("0")[0];
            buffer[4] = GetRandomNum(9);
            buffer[5] = GetRandomNum(9);
            buffer[6] = GetRandomNum(9);
            buffer[7] = GetRandomNum(9);

            //小数点
            buffer[8] = Encoding.ASCII.GetBytes("2")[0]; ;

            buffer[9] = 88;
            buffer[10] = 88;
            buffer[11] = 03;

            return buffer;
        }

         private Thread thread;

        private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            button2.Enabled = true;
            Start();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            button1.Enabled = true;
            button2.Enabled = false;
            Stop();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            button1.Enabled = true;
            button2.Enabled = false;
        }

        private void Start()
        {
            thread = new Thread(() =>
            {
                SerialPort serialPort = null;
                try
                {
                    serialPort = new SerialPort(serialPortName);
                    serialPort.BaudRate = 1200;
                    serialPort.Parity = Parity.None;
                    serialPort.StopBits = StopBits.One;
                    serialPort.DataBits = 8;
                    serialPort.Encoding = Encoding.ASCII;

                    if (serialPort.IsOpen) serialPort.Close();
                    serialPort.Open();

                    do
                    {
                        Thread.Sleep(100);
                        byte[] buffer = GetBuffer();
                        serialPort.Write(buffer, 0, buffer.Length);
                    } while (true);
                }
                catch (Exception)
                {
                    serialPort?.Dispose();
                }

            })
            { IsBackground = true };
            thread.Start();
        }

        private void Stop()
        {
            if (thread.ThreadState != ThreadState.Aborted) thread.Abort();
        }
    }
}
